# coding:utf-8
from concurrent.futures import ThreadPoolExecutor, as_completed
import pymysql
import os
import re
from pymongo import MongoClient
from bs4 import BeautifulSoup
from outline_config import OutlineConfig

conf = OutlineConfig()
def read():
    notices = getUnMakerNotices()
    pattern = re.compile('\d{4}年')
    for notice in notices:

        company_name = notice[0]
        src_id = notice[1]
        title = notice[2]
        year = pattern.match(title).group()
        industry_name = notice[3]

        html = getHtmlContent(notice[1])
        print(html)

def getUnMakerNotices():
    conn = getMysqlConn()
    cur = conn.cursor()
    sql = '''
      SELECT  c.sec_name,a.src_id, a.title,d.second_indu_code
        FROM ( SELECT * FROM notice 
                                 WHERE added_id NOT IN ( SELECT notice_id 
                                                          FROM kg_notice_related 
                                                          GROUP BY notice_id ) 
                                 AND type in ('财务报告','一般公告','重大事项','交易提示','股权股本','配股','增发') 
                                 and title LIKE '%年年度报告' 
                                 and title not LIKE '%摘要%' 
                     and title not LIKE '%预案%' 
                     and title not LIKE '%更正公告%'
										 and TO_DAYS(publish_at) = TO_DAYS('2017-11-29') or TO_DAYS(publish_at) = TO_DAYS('2017-11-30')
										 ) a 
        LEFT JOIN sec_basic_info c ON a.stockcode = c.abc_code 
        LEFT JOIN sec_industry_new d ON c.abc_code = d.stk_code 
        WHERE d.indu_standard = '1001015' 
		order by publish_at desc  '''
    cur.execute(sql)
    # 获取查询结果
    results = cur.fetchall()
    print (results)
    cur.close()
    conn.close()
    return results


# 获取开发mysql数据库连接
def getMysqlConn():
    host, port, dbname, user_name, password = conf.get_mysql_conn()
    conn = pymysql.connect(
        host=host,
        port=int(port),
        user=user_name,
        passwd=password,
        db=dbname,
        charset='utf8',
    )
    return conn
# 获取开发mysql数据库连接
def getDevMysqlConn():
    host, port, dbname, user_name, password = conf.get_mysql_dev_conn()
    conn = pymysql.connect(
        host=host,
        port=int(port),
        user=user_name,
        passwd=password,
        db=dbname,
        charset='utf8',
    )
    return conn

# 通过src_id获取MongoDB中的HTML文件
def getHtmlContent(src_id='jc_60889513'):
    db = getMongoClient()
    items = db.finance.juchao.item.find({'src_id': src_id})
    htmlText = None
    # print('mongodb result:'+str(count(items)))
    print(items)
    for item in items:
        _id = item['_id']
        texts = db.juchao_text.find({'_id': _id})
        for text in texts:
            if 'html' in text:
                htmlText = text['html']
    return htmlText

# 获取mongodb访问连接
def getMongoClient():
    dsn = conf.get_mongodb_conn()
    client = MongoClient(dsn)
    db = client.cr_data
    return db

def main():
    futures = set()
    with ThreadPoolExecutor(2) as executor:
        notices = getUnMakerNotices()
        for notice in notices:
            company_name = notice[0]
            src_id = notice[1]
            title = notice[2]
            year = re.findall(r"\d{4}年.*", title)
            year = year[0] if len(year) > 0 else '2016年'
            industry_code = notice[3]
            text_name = src_id+'_'+company_name+'_'+year+'_'+industry_code
            html = getHtmlContent(notice[1])
            future = executor.submit(writeText, text_name, html)
            futures.add(future)
    try:
        for future in as_completed(futures):
            err = future.exception()
            if err is not None:
                raise err
    except KeyboardInterrupt:
        print("stopped by hand")

def writeText(text_name, html):
    print(text_name)
    project_path = conf.get_project_path()
    path = project_path+str(text_name) + '.txt'
    if os.path.exists(path) is False:
        file_object = open(path, 'w+', encoding='utf-8')
        if html:
            file_object.write(html)
        file_object.close()

if __name__ == '__main__':
    main()
